<?php
namespace app\web\model;

use think\Db;
use app\lib\model\Price;
use Yansongda\Pay\Pay;
use app\lib\exception\OrderException;
/**
 * 意见反馈
 * @package app\basic\model
 */
class Order extends Base 
{
    protected function getCreateTimeAttr($v){
        return date('Y-m-d H:i',$v);
    }
    protected function getOrderTimeAttr($v){
        return date('Y-m-d H:i',$v);
    }
    /**
     * 计算订单 商家应抽佣金额
     * [shopCommission description]
     * @param  [type] $order [description]
     * @return [type]        [description]
     */
    public static function shopCommission($order)
    {
        if($order['shop_id'] == 0) return 0;
        $shop_order_commission = 0;
        $technician = Technician::field('id,shop_order_rate,shop_plus_rate')->get($order['technician_id']);
        if($technician['shop_order_rate'] > 0){
            $shop_order_commission = Price::pricecalc($order['service_money'],'*',$technician['shop_order_rate']);
        }
        $coupon = Coupon::alias('a')->leftjoin('coupon_type b','b.id=a.type_id')->where('a.id',$order['coupon_id'])->value('b.coupon_type');
        $coupon_money = $coupon == 1 ? $order['coupon_money'] : 0;
        return Price::pricecalc($shop_order_commission,'-',$coupon_money);
    }
    /**
     * 平台可得佣金
     * [platformCommission description]
     * @param  [type] $order [description]
     * @return [type]        [description]
     */
    public static function platformCommission($order){
        $technician = Technician::field('id,order_rate,plus_rate')->get($order['technician_id']);
        $order_commission = 0;
        $plus_commission = 0;
        if($technician['order_rate'] > 0){
            $order_commission = Price::pricecalc($order['service_money'],'*',$technician['order_rate']);
        }
        $coupon = Coupon::alias('a')->leftjoin('coupon_type b','b.id=a.type_id')->where('a.id',$order['coupon_id'])->value('b.coupon_type');
        $coupon_money = !is_null($coupon) && $coupon == 0 ? $order['coupon_money'] : 0;
        return Price::pricecalc($order_commission,'-',$coupon_money);
    }
    /**
     * 完成订单赠送积分 计算佣金
     * [completeOrder description]
     * @param  [type] $order_id [description]
     * @return [type]           [description]
     */
    public static function completeOrder($order_id)
    {
        $order = self::get($order_id);
        if($order->status == 4) return true;
        $order->status = 4;
        $order->shop_status = 2;
        $order->complete_time = time();
        Db::startTrans();
        $res = $order->save();
        self::log('订单完成'.$order_id);
        if(!$res) return false;
        if($order->shop_id > 0){
            $res = Shop::where('id',$order->shop_id)->setInc('deal_number');
            if(!$res){
                Db::rollback();
                self::log('店铺增加订单完成数量失败');
                return false;
            }
        }
        $res = OrderLogs::create([
            'order_id' => $order_id,
            'contents0' => '用户确认消费，订单服务完成',
            'contents1' => '用户确认消费，订单服务完成',
            'contents2' => '用户确认消费，订单服务完成',
            'mobile' => $order->mobile
        ]);
        if($res->isEmpty()){
            Db::rollback();
            self::log('日志记录失败');
            return false;
        }
        //消费赠送积分
        $order_integral_rate = Config::getConfig('order_integral_rate');
        $change_integral = Price::pricecalc($order->tot_money,'*',$order_integral_rate,0);
        $res = UserIntegral::change($order->user_id,$change_integral,0,$order_id);
        if(!$res){
            Db::rollback();
            self::log('积分赠送失败');
            return false;
        }

        $sub_ordinary_rate = Config::getConfig('sub_ordinary_rate');
        $sub_technician_rate = Config::getConfig('sub_technician_rate');
        $sub_shop_rate = Config::getConfig('sub_shop_rate');
        //上级用户
        $top_ordinary_user = User::where(['id'=>$order->user_id])->value('p_id');
        if($top_ordinary_user > 0){
            $change_balance = Price::pricecalc($order->tot_money,'*',$sub_ordinary_rate);
            $res = UserBalance::change($top_ordinary_user,$change_balance,3,'分享用户消费成功',$order_id);
            if(!$res){
                Db::rollback();
                self::log('分享用户消费失败');
                return false;
            }
        }
        //技师上级用户
        $top_technician_user = Technician::alias('a')->leftjoin('user b','b.id=a.user_id')->where(['a.id'=>$order->technician_id])->value('b.p_id');
        if($top_technician_user > 0){
            $change_balance = Price::pricecalc($order->tot_money,'*',$sub_technician_rate);
            $res = UserBalance::change($top_technician_user,$change_balance,3,'分享技师完成订单获得佣金',$order_id);
            if(!$res){
                Db::rollback();
                self::log('分享技师完成订单获得佣金失败');
                return false;
            }
        }
        //店铺上级用户
        if($order->shop_id > 0){
            $top_shop_user = Shop::alias('a')->leftjoin('user b','b.id=a.user_id')->where('a.id',$order->shop_id)->value('b.p_id');
            if($top_shop_user > 0){
                $change_balance = Price::pricecalc($order->tot_money,'*',$sub_shop_rate);
                $res = UserBalance::change($top_shop_user,$change_balance,3,'分享商家完成抽佣获得佣金',$order_id);
                if(!$res){
                    Db::rollback();
                    self::log('分享商家完成抽佣获得佣金失败');
                    return false;
                }
            }
        }
        //发放店铺佣金
        $res = self::grantShopCommission($order_id,$order->shop_id);
        if(!$res){
            Db::rollback();
            self::log('发放店铺佣金失败');
            return false;
        }
        //发放技师佣金
        $res = self::grantTeCommission($order_id,$order->technician_id);
        if($res === false){
            Db::rollback();
            self::log('发放技师佣金失败');
            return false;
        }
        Db::commit();
        return true;
    }
    /**
     * 发放技师佣金
     * [grantTeCommission description]
     * @param  [type] $order_id [description]
     * @param  [type] $t_id     [description]
     * @return [type]           [description]
     */
    private static function grantTeCommission($order_id,$t_id)
    {
        if($t_id == 0) return true;
        $data = TechnicianBalance::where(['t_id'=>$t_id,'order_id'=>$order_id,'status'=>0,'balance_type'=>1])->find();
        if(!$data) return true;
        $shop = Technician::get($t_id);
        $data->before_balance = $shop->balance;
        $data->after_balance = Price::pricecalc($shop->balance,'+',$data->change_balance);
        $data->status = 1;
        $data->notes = '订单已结算';
        $shop->balance = $data->after_balance;
        $data->save();
        $shop->save();
        return true;
    }
    /**
     * 发放店铺端佣金
     * [grantShopCommission description]
     * @param  [type] $order_id [description]
     * @param  [type] $shop_id  [description]
     * @return [type]           [description]
     */
    private static function grantShopCommission($order_id,$shop_id)
    {
        if($shop_id == 0) return true;
        $data = ShopBalance::where(['shop_id'=>$shop_id,'order_id'=>$order_id,'status'=>0,'balance_type'=>1])->find();
        if(!$data) return true;
        $shop = Shop::get($shop_id);
        $data->before_balance = $shop->balance;
        $data->after_balance = Price::pricecalc($shop->balance,'+',$data->change_balance);
        $data->status = 1;
        $shop->balance = $data->after_balance;
        $data->save();
        $shop->save();
        return true;
    }
    /**
     * 取消订单
     * [cancelOrder description]
     * @param  [type]  $order_id [description]
     * @param  integer $appkey   [description]
     * @return [type]            [description]
     */
    public static function cancelOrder($order_id,$appkey = 0)
    {
        $order = self::alias('a')
                            ->where('a.id',$order_id)
                            ->field('a.id,a.scheduling_id,a.order_no,a.shop_id,a.technician_id,a.user_id,a.order_time,c.shop_mobile as t_mobile,ifnull(d.shop_mobile,"") as shop_mobile,a.pay_money,a.coupon_id,a.integral,a.pay_status,a.balance_money,a.status,b.openid,a.is_refund,a.order_pay_no,a.order_time,a.travel_price,a.pay_type,a.mobile,a.tot_money')
                            ->leftjoin('user b','b.id=a.user_id')
                            ->leftjoin('technician c','c.id=a.technician_id')
                            ->leftjoin('shop d','d.id=c.shop_id')
                            ->find();
        if($order->status == 5){
            throw new OrderException(['code'=>400,'msg'=>'该订单已取消']);
        }
        self::log('取消订单:'.$order['order_no']);
        if($order->status < 2){   //未接订单全额退款
            if($order->pay_money > 0){  //先退微信支付金额
                $res = self::returnWx($order['order_pay_no'],$order['pay_money'],$order['pay_money'],$order['user_id']);
                if(!$res){
                    self::log('微信退款失败'.$res);
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }
            Db::startTrans();
            if($order->balance_money > 0){
                $res = UserBalance::change($order->user_id,$order->balance_money,2,'用户取消订单',$order->id);
                if(!$res){
                    Db::rollback();
                    self::log('余额退款失败');
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }
            if($order->coupon_id > 0){
                $res = Coupon::returnCoupon($order->coupon_id);
                if(!$res){
                    Db::rollback();
                    self::log('优惠券退款失败');
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }
            if($order->integral > 0){
                $res = UserIntegral::change($order->user_id,$order->integral,2,$order->id);
                if(!$res){
                    Db::rollback();
                    self::log('积分退款失败');
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }
            $order_logs = OrderLogs::create([
                'logs_type' => 1,
                'order_id' => $order->id,
                'contents0' => ($appkey == 0 ? '客户' : ($appkey == 1 ? '技师' : '商户')) . '取消订单',
                'contents1' => ($appkey == 0 ? '客户' : ($appkey == 1 ? '技师' : '商户')) . '取消订单',
                'contents2' => ($appkey == 0 ? '客户' : ($appkey == 1 ? '技师' : '商户')) . '取消订单',
                'mobile' => $appkey == 0 ? $order->mobile : ($appkey == 1 ? $order->t_mobile : $order->shop_mobile)
            ]);
            if($order_logs->isEmpty()){
                Db::rollback();
                self::log('订单日志保存失败');
                throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
            }
            $order->status = 5;
            $order->technician_status = 3;
            $order->shop_status = 3;
            $order->is_refund = 1;
            $order->refund_time = time();  //退款时间
            $order->refund_desc = '未接订单全额退款';
            $order->refund_appkey = $appkey;
            $res = $order->save();
            if(!$res){
                Db::rollback();
                self::log('修改订单数据失败');
                throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
            }
            $res = Scheduling::where(['order_id'=>$order->id])->update(['status'=>0,'order_id'=>0]);
            if($res === false){
                Db::rollback();
                self::log('修改排班数据失败');
                throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
            }
            Db::commit();

            $model = new \app\lib\model\Ylysms();
            $rest = $model->sendTemplateSMS($order['mobile'],[],config('api.ylysms_cancel_order_id'));

            return true;
        }
        $platform_service_rate = Config::getConfig('platform_service_rate');  //Y%   平台服务费率
        //该单应该扣除的平台服务费
        $platform_service_money = Price::pricecalc($order->tot_money,'*',$platform_service_rate);

        //已接单订单取消
        if($appkey == 1 || $appkey == 2){  //技师或者商户取消订单 用户支付金额全部退回
            if($order->pay_money > 0){  //先退微信支付金额
                $res = self::returnWx($order['order_pay_no'],$order['pay_money'],$order['pay_money'],$order['user_id']);
                if(!$res){
                    self::log('微信退款失败'.$res);
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }
            Db::startTrans();
            if($order->shop_id > 0){   //商家订单 商家退款率
                $res = self::shopRefundRate($order->shop_id);
                if(!$res){
                    Db::rollback();
                    self::log('商家订单取消商家退款率');
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }
            
            //扣除技师或者商家的平台服务费
            if($appkey == 2){
                $res = ShopBalance::change($order->shop_id,-$platform_service_money,0,'商户取消订单',$order->id);
                if(!$res){
                    Db::rollback();
                    self::log('扣除商户平台服务费失败');
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }else{
                $res = TechnicianBalance::change($order->technician_id,-$platform_service_money,0,'技师取消订单',$order->id);
                if(!$res){
                    Db::rollback();
                    self::log('扣除技师平台服务费失败');
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }

            if($order->balance_money > 0){
                $res = UserBalance::change($order->user_id,$order->balance_money,2,($appkey == 2 ? '商户' : '技师').'取消订单',$order->id);
                if(!$res){
                    Db::rollback();
                    self::log('余额退款失败');
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }
            if($order->coupon_id > 0){
                $res = Coupon::returnCoupon($order->coupon_id);
                if(!$res){
                    Db::rollback();
                    self::log('优惠券退款失败');
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }
            if($order->integral > 0){
                $res = UserIntegral::change($order->user_id,$order->integral,2,$order->id);
                if(!$res){
                    Db::rollback();
                    self::log('积分退款失败');
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }
            $order_logs = OrderLogs::create([
                'logs_type' => 1,
                'order_id' => $order->id,
                'contents0' => ($appkey == 0 ? '客户' : ($appkey == 1 ? '技师' : '商户')) . '取消订单',
                'contents1' => ($appkey == 0 ? '客户' : ($appkey == 1 ? '技师' : '商户')) . '取消订单',
                'contents2' => ($appkey == 0 ? '客户' : ($appkey == 1 ? '技师' : '商户')) . '取消订单',
                'mobile' => $appkey == 0 ? $order->mobile : ($appkey == 1 ? $order->t_mobile : $order->shop_mobile)
            ]);
            if($order_logs->isEmpty()){
                Db::rollback();
                self::log('订单日志保存失败');
                throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
            }
            $order->status = 5;
            $order->technician_status = 3;
            $order->shop_status = 3;
            $order->is_refund = 1;
            $order->refund_time = time();  //退款时间
            $order->refund_desc = ($appkey == 1 ? '技师' : '商户') .'取消订单全额退款';
            $order->refund_appkey = $appkey;
            $order->platform_service_money = $platform_service_money;
            $res = $order->save();
            if(!$res){
                Db::rollback();
                self::log('修改订单数据失败');
                throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
            }
            $res = Scheduling::where(['order_id'=>$order->id])->update(['status'=>0,'order_id'=>0]);
            if(!$res){
                Db::rollback();
                self::log('修改排班数据失败');
                throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
            }
            Db::commit();
            $model = new \app\lib\model\Ylysms();
            $send_mobile = $order['mobile'];
            $send_mobile .= ','.$order['t_mobile'];
            if($order['shop_mobile']){
                $send_mobile .= ','.$order['shop_mobile'];
            }
            $rest = $model->sendTemplateSMS($send_mobile,[],config('api.ylysms_cancel_order_id'));
            return true;
        }
        //用户取消订单
        $order_hour = Config::getConfig('order_hour');   //N小时 
        $order_time = strtotime($order->order_time);
        $is_order_hour = $order_time - $order_hour > time();  //false 小于N小时  ture大于N小时
        $refund_travel_price = 0;    //路费
        $delay_work_money = 0;       //技师误工费
        if(!$is_order_hour){   //小于N小时 扣除技师误工费和路费
            $refund_travel_price = $order->travel_price;
            $delay_work_money = Config::getConfig('delay_work_money');   //技师误工费
        }
        $tot_platform = Price::pricecalc(Price::pricecalc($platform_service_money,'+',$refund_travel_price),'+',$delay_work_money);   //取消订单应扣除的总费用
        
        if($order->pay_money > 0){  //先退微信支付金额
            $return_money = Price::pricecalc($order['pay_money'],'-',$tot_platform);
            if($return_money > 0){
                $res = self::returnWx($order['order_pay_no'],$order['pay_money'],$return_money,$order['user_id']);
                if(!$res){
                    self::log('微信退款失败'.$res);
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            }
        }
        Db::startTrans();
        if($order->balance_money > 0){
            $return_money = Price::pricecalc($order['balance_money'],'-',$tot_platform);
            if($return_money > 0){
                $res = UserBalance::change($order->user_id,$return_money,2,'用户取消订单',$order->id);
                if(!$res){
                    Db::rollback();
                    self::log('余额退款失败');
                    throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
                }
            } 
        }
        if($order->shop_id > 0){   //商家订单 商家退款率
            $res = self::shopRefundRate($order->shop_id);
            if(!$res){
                Db::rollback();
                self::log('商家订单取消商家退款率');
                throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
            }
        }
            
        if($order->coupon_id > 0){
            $res = Coupon::returnCoupon($order->coupon_id);
            if(!$res){
                Db::rollback();
                self::log('优惠券退款失败');
                throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
            }
        }
        if($order->integral > 0){
            $res = UserIntegral::change($order->user_id,$order->integral,2,$order->id);
            if(!$res){
                Db::rollback();
                self::log('积分退款失败');
                throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
            }
        }
        $order_logs = OrderLogs::create([
            'logs_type' => 1,
            'order_id' => $order->id,
            'contents0' => ($appkey == 0 ? '客户' : ($appkey == 1 ? '技师' : '商户')) . '取消订单',
            'contents1' => ($appkey == 0 ? '客户' : ($appkey == 1 ? '技师' : '商户')) . '取消订单',
            'contents2' => ($appkey == 0 ? '客户' : ($appkey == 1 ? '技师' : '商户')) . '取消订单',
            'mobile' => $appkey == 0 ? $order->mobile : ($appkey == 1 ? $order->t_mobile : $order->shop_mobile)
        ]);
        if($order_logs->isEmpty()){
            Db::rollback();
            self::log('订单日志保存失败');
            throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
        }
        $order->status = 5;
        $order->technician_status = 3;
        $order->shop_status = 3;
        $order->is_refund = 1;
        $order->refund_time = time();  //退款时间
        $order->refund_desc = '客户原因退款';
        $order->refund_appkey = $appkey;
        $order->delay_work_money = $delay_work_money;
        $order->platform_service_money = $platform_service_money;
        $order->refund_travel_price = $refund_travel_price;
        if($refund_travel_price == 0){   //用户取消订单退款 技师路费
            $order->is_settlement_travel = 1;
        }else{
            $order->is_settlement_travel = 0;
        }
        $order->tot_platform = $tot_platform;
        $res = $order->save();
        if(!$res){
            Db::rollback();
            self::log('修改订单数据失败');
            throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
        }
        if($delay_work_money > 0){  //用户取消订单 产生的技师误工费直接打给技师账户
            $res = TechnicianBalance::change($order->technician_id,$delay_work_money,5,'用户取消订单技师误工费用',$order->id,);
            if(!$res){
            Db::rollback();
            self::log('技师误工费结算');
            throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
        }
        }
        $res = Scheduling::where(['order_id'=>$order->id])->update(['status'=>0,'order_id'=>0]);
        if(!$res){
            Db::rollback();
            self::log('修改排班数据失败');
            throw new OrderException(['code'=>400,'msg'=>'取消订单失败']);
        }
        Db::commit();
        $model = new \app\lib\model\Ylysms();
        $send_mobile = $order['mobile'];
        $send_mobile .= ','.$order['t_mobile'];
        if($order['shop_mobile']){
            $send_mobile .= ','.$order['shop_mobile'];
        }
        $rest = $model->sendTemplateSMS($send_mobile,[],config('api.ylysms_cancel_order_id'));
   
        return true;

    }
    /**
     * 计算商家的退款率
     * @param  [type] $shop_id [description]
     * @return [type]          [description]
     */
    private static function shopRefundRate($shop_id){
        $tot = self::where(['shop_id'=>$shop_id])->count();
        $c_tot = self::where(['shop_id'=>$shop_id,'status'=>5])->count();
        $shop = Shop::get($shop_id);
        $shop->refund_rate = Price::pricecalc(100,'*',Price::pricecalc($c_tot+1,'/',$tot),0);
        $res = $shop->save();
        return $res ? true : false;
    }
    private static function log($data)
    {
        file_log($data,'runtime/log/order');
    }
    /**
     * 微信退款
     * [returnWx description]
     * @param  [type] $order_pay_no [description]  支付单号
     * @param  [type] $total_fee    [description]  微信支付金额
     * @param  [type] $refund_fee   [description]  微信退款金额
     * @return [type]               [description]
     */
    private static function returnWx($order_pay_no,$total_fee,$refund_fee,$user_id)
    {
        $orderData = [
            'out_trade_no'   => $order_pay_no,
            'out_refund_no'  => $order_pay_no.rand(1000,9999),
            'total_fee'      => Price::priceyuantofen($total_fee),
            'refund_fee'     => Price::priceyuantofen($refund_fee),
            'refund_desc'    => '用户退款',
            'type'           => 'miniapp'
        ];
        //实际退款
        try{
            $result = Pay::wechat(Config::getWechatConfig(0))->refund($orderData);
        } catch (\Exception $e){
            return $e->getMessage();
        }
        if($result['return_code'] != 'SUCCESS'){
            return $result['return_msg'];
        }
        $user = User::find($user_id);
        if(!Platform::change([
            'role' => 0,
            'p_id' => $user_id,
            'planform_type' => 3,
            'mobile' => $user->mobile,
            'user_name' => $user->nickName,
            'change_balance' => -$refund_fee,
            'notes' => '用户微信退款'
        ])) return false;
        return true;
    }
    //+==========================================================定时任务==========================================
    /**
     * 定时任务自动完成
     * [taskCompleteOrder description]
     * @return [type] [description]
     */
    public static function taskCompleteOrder()
    {
        $order_complete_time = Config::getConfig('order_complete_time');
        $time = time();
        $list = self::where([['status','=',3],['technician_status','=',2],['technician_complete_time','<',$time-$order_complete_time]])
                        ->field('id')
                        ->select()->toArray();
        if(!$list) return true;
        foreach ($list as $key => $value) {
            $res = self::completeOrder($value['id']);
            if(!$res) return false;
        }
        return true;
    }
    /**
     * 定时任务自动取消
     * [taskCancelOrder description]
     * @return [type] [description]
     */
    public static function taskCancelOrder()
    {
        //N秒未支付订单自动取消
        $order_cancel_time = Config::getConfig('order_cancel_time');
        //N秒为确认订单自动取消
        $unconfirmed_times = Config::getConfig('unconfirmed_times');
        $time = time();
        $where = '(status=0 and pay_status=0 and create_time<'.($time-$order_cancel_time).') or (status<2 and create_time<'.($time-$unconfirmed_times).')';
        $list = self::where($where)
                        ->field('id,coupon_id,mobile')
                        ->select()
                        ->toArray();
        if(!$list) return true;
        foreach ($list as $key => $value) {
            self::cancelOrder($value['id']);
        }
        return true;
    }
    /**
     * 技师N天未打卡自动下线
     * [offline description]
     * @return [type] [description]
     */
    public static function offline()
    {
        $offline_day = Config::getConfig('offline_day');

        $data = Technician::where([['sign_time','<',time()-$offline_day],['check_status','=',1],['is_online','=',1]])->column('shop_mobile');
        //短信通知
        $ylysms_no_location_id = config('api.ylysms_no_location_id');
        if(!$data && $ylysms_no_location_id){
            $day = (int)($offline_day/86400);
            (new \app\lib\model\Ylysms())->sendTemplateSMS(implode(',',$data),[$day],$ylysms_no_location_id);
        }
        $res = Technician::where([['sign_time','<',time()-$offline_day],['check_status','=',1],['is_online','=',1]])->update(['is_online'=>0]);
        if($res === false) return false;
        return true;
    }
    /**
     * 服务开始前N分钟通知技师
     * 定时发送短信通知技师
     * [taskSend description]
     * @return [type] [description]
     */
    public static function taskSend()
    {
        $service_start_before = Config::getConfig('service_start_before');
        $t = time()-$service_start_before;
        $data = self::alias('a')
                    ->where([['a.status','=',2],['technician_status','=',1],['technician_service','=',0],['a.is_sms','=',0],['a.order_time','<=',$t]])
                    ->field('a.id,b.shop_mobile')
                    ->leftjoin('technician b','b.id=a.technician_id')
                    ->select()->toArray();
        if(!$data) return true;
        //发送短信通知技师
        $res = (new \app\lib\model\Ylysms())->sendTemplateSMS(implode(',',array_unique(array_column($data,'shop_mobile'))),[],config('api.ylysms_before_service_id'));
        $r = self::where('id','in',array_column($data, 'id'))->update(['is_sms'=>1]);
        return json_encode($res).'=>'.$r;
    }
}